package E2019;

import java.io.*;
public class Exam1 {
    public static void main(String[] args) {
        File file = new File("G://HelloWorld.txt");
        // 创建文件
        if (!file.exists()){
            try {
                file.createNewFile();
                System.out.println("创建文件成功");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        // 判断文件
        if (file.isDirectory()){
            System.out.println("这是个目录");
        }else {
            System.out.println("这是一个文件");
        }
        // 创建文件夹
        File dir = new File("G://IOTest");
        if (!dir.exists()){
            dir.mkdir();
        }
        FileInputStream in = null;
        FileOutputStream out = null;
        try {
             in = new FileInputStream("G://HelloWorld.txt");
             out = new FileOutputStream("G://IOTest//HelloWorld.txt");
            int len = 0;
            while ((len=in.read())!=-1){
                out.write(len);
            }
            System.out.println("文件移动成功");
            // 遍历输出文件
            System.out.println("IOTest目录下的文件如下：");
            File[] list = dir.listFiles();
            for (File file1:list){
                System.out.println(file1.getName());
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (in!=null){
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (out!=null){
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
